﻿using Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Admin
{
    public partial class View_Result : Form
    {
        List<Stu_group> stu_Groups = new List<Stu_group>();
        public View_Result()
        {
            InitializeComponent();
        }

        // private void View_Result_Load(object sender, EventArgs e)
        // {
        //     BLL.B_Group b_Group = new BLL.B_Group();
        //     stu_Groups = b_Group.getList();
        //     stu_Groups= stu_Groups.OrderBy(g => g.group_id).ToList();
        //     Groups_view.DataSource = stu_Groups;
        // }

        // private void View_Result_Load(object sender, EventArgs e)
        // {
        //     BLL.B_Group b_Group = new BLL.B_Group();
        //     stu_Groups = b_Group.getList();
        //     stu_Groups = stu_Groups.OrderBy(g => g.group_id).ToList();
        //
        //     // Fill grade ComboBox
        //     var grades = stu_Groups.Select(g => g.grade.ToString()).Distinct().ToList();
        //     comboBoxGrade.DataSource = grades;
        //     comboBoxGrade.ValueMember = "";
        //     comboBoxGrade.DisplayMember = "";
        //
        //     // Fill major_id ComboBox
        //     var majors = stu_Groups.Select(g => g.major_id.ToString()).Distinct().ToList();
        //     comboBoxMajor.DataSource = majors;
        //     comboBoxMajor.ValueMember = "";
        //     comboBoxMajor.DisplayMember = "";
        // }
        private void View_Result_Load(object sender, EventArgs e)
        {
            BLL.B_Group b_Group = new BLL.B_Group();
            stu_Groups = b_Group.getList();
            stu_Groups = stu_Groups.OrderBy(g => g.group_id).ToList();
            Groups_view.DataSource = stu_Groups;

            var grades = new List<string> { "" }.Concat(stu_Groups.Select(g => g.grade.ToString()).Distinct()).ToList();
            comboBoxGrade.DataSource = grades;
            comboBoxGrade.SelectedIndex = 0; // 设置默认选项

            var majors = new List<string> { "" }.Concat(stu_Groups.Select(g => g.major_id.ToString()).Distinct()).ToList();
            comboBoxMajor.DataSource = majors;
            comboBoxMajor.SelectedIndex = 0; // 设置默认选项
        }


        //      private void RefreshGroupsView()
        //      {
        //          var filteredGroups = stu_Groups.Where(g =>
        //      (comboBoxGrade.SelectedItem == null || g.grade.ToString() == (comboBoxGrade.SelectedItem.ToString())) &&
        //      (comboBoxMajor.SelectedItem == null || g.major_id.ToString() == (comboBoxMajor.SelectedItem.ToString()))
        //  ).ToList();
        //
        //          Groups_view.DataSource = filteredGroups;
        //      }

        private void RefreshGroupsView()
        {
            // 检查 SelectedItem 是否为 null
            string selectedGrade = comboBoxGrade.SelectedItem != null ? comboBoxGrade.SelectedItem.ToString() : "";
            string selectedMajor = comboBoxMajor.SelectedItem != null ? comboBoxMajor.SelectedItem.ToString() : "";

            var filteredGroups = stu_Groups.Where(g =>
                (selectedGrade == "" || g.grade.ToString() == selectedGrade) &&
                (selectedMajor == "" || g.major_id.ToString() == selectedMajor)
            ).ToList();

            Groups_view.DataSource = filteredGroups;
        }

        private void comboBoxGrade_SelectedIndexChanged(object sender, EventArgs e)
        {
            RefreshGroupsView();
        }

        private void comboBoxMajor_SelectedIndexChanged(object sender, EventArgs e)
        {
            RefreshGroupsView();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // 获取搜索框中的文本
            string searchQuery = textBox1.Text.Trim(); // 获取文本框中的搜索文本，并去除前后空格

            // 获取当前选中的年级和专业
            string selectedGrade = comboBoxGrade.SelectedItem != null ? comboBoxGrade.SelectedItem.ToString() : "";
            string selectedMajor = comboBoxMajor.SelectedItem != null ? comboBoxMajor.SelectedItem.ToString() : "";

            // 如果搜索文本为空，根据当前选中的年级和专业显示数据
            if (string.IsNullOrEmpty(searchQuery))
            {
                var groupsByGradeAndMajor = stu_Groups.Where(g =>
                    (selectedGrade == "" || g.grade.ToString() == selectedGrade) &&
                    (selectedMajor == "" || g.major_id.ToString() == selectedMajor)
                ).ToList();
                Groups_view.DataSource = groupsByGradeAndMajor;
            }
            else
            {
                // 根据搜索框中的文本进行模糊搜索，区分大小写
                var filteredGroups = stu_Groups
                    .Where(g => g.project_name != null && g.project_name.IndexOf(searchQuery) >= 0)
                    .ToList();

                // 应用其他过滤条件
                filteredGroups = filteredGroups.Where(g =>
                    (selectedGrade == "" || g.grade.ToString() == selectedGrade) &&
                    (selectedMajor == "" || g.major_id.ToString() == selectedMajor)
                ).ToList();

                // 如果搜索结果不为空，更新表格显示
                if (filteredGroups.Count > 0)
                {
                    Groups_view.DataSource = filteredGroups;
                }
                else
                {
                    
                    // 如果搜索结果为空，根据当前选中的年级和专业显示数据
                    var groupsByGradeAndMajor = stu_Groups.Where(g =>
                        (selectedGrade == "" || g.grade.ToString() == selectedGrade) &&
                        (selectedMajor == "" || g.major_id.ToString() == selectedMajor)
                    ).ToList();
                    Groups_view.DataSource = groupsByGradeAndMajor;
                    MessageBox.Show("找不到符合条件的项目");
                }
            }
        }

        private void uiButton3_Click(object sender, EventArgs e)
        {
            // 获取搜索框中的文本
            string searchQuery = textBox1.Text.Trim(); // 获取文本框中的搜索文本，并去除前后空格

            // 获取当前选中的年级和专业
            string selectedGrade = comboBoxGrade.SelectedItem != null ? comboBoxGrade.SelectedItem.ToString() : "";
            string selectedMajor = comboBoxMajor.SelectedItem != null ? comboBoxMajor.SelectedItem.ToString() : "";

            // 如果搜索文本为空，根据当前选中的年级和专业显示数据
            if (string.IsNullOrEmpty(searchQuery))
            {
                var groupsByGradeAndMajor = stu_Groups.Where(g =>
                    (selectedGrade == "" || g.grade.ToString() == selectedGrade) &&
                    (selectedMajor == "" || g.major_id.ToString() == selectedMajor)
                ).ToList();
                Groups_view.DataSource = groupsByGradeAndMajor;
            }
            else
            {
                // 根据搜索框中的文本进行模糊搜索，区分大小写
                var filteredGroups = stu_Groups
                    .Where(g => g.project_name != null && g.project_name.IndexOf(searchQuery) >= 0)
                    .ToList();

                // 应用其他过滤条件
                filteredGroups = filteredGroups.Where(g =>
                    (selectedGrade == "" || g.grade.ToString() == selectedGrade) &&
                    (selectedMajor == "" || g.major_id.ToString() == selectedMajor)
                ).ToList();

                // 如果搜索结果不为空，更新表格显示
                if (filteredGroups.Count > 0)
                {
                    Groups_view.DataSource = filteredGroups;
                }
                else
                {

                    // 如果搜索结果为空，根据当前选中的年级和专业显示数据
                    var groupsByGradeAndMajor = stu_Groups.Where(g =>
                        (selectedGrade == "" || g.grade.ToString() == selectedGrade) &&
                        (selectedMajor == "" || g.major_id.ToString() == selectedMajor)
                    ).ToList();
                    Groups_view.DataSource = groupsByGradeAndMajor;
                    MessageBox.Show("找不到符合条件的项目");
                }
            }
        }
    }
}
